package com.ruoyi.report_lq.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.syrw.annotation.FIllBasicInfo;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.report_lq.domain.bo.ReportRhlqYcjldnfxjlbBsInfoBo;
import com.ruoyi.report_lq.domain.vo.ReportRhlqYcjldnfxjlbBsInfoVo;
import com.ruoyi.report_lq.domain.ReportRhlqYcjldnfxjlbBsInfo;
import com.ruoyi.report_lq.mapper.ReportRhlqYcjldnfxjlbBsInfoMapper;
import com.ruoyi.report_lq.service.IReportRhlqYcjldnfxjlbBsInfoService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 【请填写功能名称】Service业务层处理
 *
 * @author zyb
 * @date 2024-07-02
 */
@RequiredArgsConstructor
@Service
public class ReportRhlqYcjldnfxjlbBsInfoServiceImpl implements IReportRhlqYcjldnfxjlbBsInfoService {

    private final ReportRhlqYcjldnfxjlbBsInfoMapper baseMapper;

    private final ReportRhlqYcjldnfxjlbBaDetailServiceImpl detailService;

    /**
     * 查询【请填写功能名称】
     */
    @Override
    @FIllBasicInfo
    public ReportRhlqYcjldnfxjlbBsInfoVo queryById(Long excelId){
        ReportRhlqYcjldnfxjlbBsInfoVo info = baseMapper.selectVoById(excelId);
        info.setNfxList(detailService.queryByInfoId(excelId));
        return info;
    }

    /**
     * 查询【请填写功能名称】列表
     */
    @Override
    public TableDataInfo<ReportRhlqYcjldnfxjlbBsInfoVo> queryPageList(ReportRhlqYcjldnfxjlbBsInfoBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<ReportRhlqYcjldnfxjlbBsInfo> lqw = buildQueryWrapper(bo);
        Page<ReportRhlqYcjldnfxjlbBsInfoVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询【请填写功能名称】列表
     */
    @Override
    public List<ReportRhlqYcjldnfxjlbBsInfoVo> queryList(ReportRhlqYcjldnfxjlbBsInfoBo bo) {
        LambdaQueryWrapper<ReportRhlqYcjldnfxjlbBsInfo> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<ReportRhlqYcjldnfxjlbBsInfo> buildQueryWrapper(ReportRhlqYcjldnfxjlbBsInfoBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ReportRhlqYcjldnfxjlbBsInfo> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getTotalPage() != null, ReportRhlqYcjldnfxjlbBsInfo::getTotalPage, bo.getTotalPage());
        lqw.eq(bo.getPage() != null, ReportRhlqYcjldnfxjlbBsInfo::getPage, bo.getPage());
        lqw.eq(StringUtils.isNotBlank(bo.getJilbh()), ReportRhlqYcjldnfxjlbBsInfo::getJilbh, bo.getJilbh());
        lqw.eq(StringUtils.isNotBlank(bo.getJiancdwmc()), ReportRhlqYcjldnfxjlbBsInfo::getJiancdwmc, bo.getJiancdwmc());
        lqw.eq(StringUtils.isNotBlank(bo.getGongcbwyt()), ReportRhlqYcjldnfxjlbBsInfo::getGongcbwyt, bo.getGongcbwyt());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpmc()), ReportRhlqYcjldnfxjlbBsInfo::getYangpmc, bo.getYangpmc());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpbh()), ReportRhlqYcjldnfxjlbBsInfo::getYangpbh, bo.getYangpbh());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpsl()), ReportRhlqYcjldnfxjlbBsInfo::getYangpsl, bo.getYangpsl());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpzt()), ReportRhlqYcjldnfxjlbBsInfo::getYangpzt, bo.getYangpzt());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpsj()), ReportRhlqYcjldnfxjlbBsInfo::getYangpsj, bo.getYangpsj());
        lqw.eq(StringUtils.isNotBlank(bo.getShiyjcrq()), ReportRhlqYcjldnfxjlbBsInfo::getShiyjcrq, bo.getShiyjcrq());
        lqw.eq(StringUtils.isNotBlank(bo.getShiytjwd()), ReportRhlqYcjldnfxjlbBsInfo::getShiytjwd, bo.getShiytjwd());
        lqw.eq(StringUtils.isNotBlank(bo.getShiytjsd()), ReportRhlqYcjldnfxjlbBsInfo::getShiytjsd, bo.getShiytjsd());
        lqw.eq(StringUtils.isNotBlank(bo.getJiancyj()), ReportRhlqYcjldnfxjlbBsInfo::getJiancyj, bo.getJiancyj());
        lqw.eq(StringUtils.isNotBlank(bo.getPandyj()), ReportRhlqYcjldnfxjlbBsInfo::getPandyj, bo.getPandyj());
        lqw.eq(StringUtils.isNotBlank(bo.getZhuyyqsbmcjbh()), ReportRhlqYcjldnfxjlbBsInfo::getZhuyyqsbmcjbh, bo.getZhuyyqsbmcjbh());
        lqw.eq(StringUtils.isNotBlank(bo.getFujsm()), ReportRhlqYcjldnfxjlbBsInfo::getFujsm, bo.getFujsm());
        lqw.eq(StringUtils.isNotBlank(bo.getShebIds()), ReportRhlqYcjldnfxjlbBsInfo::getShebIds, bo.getShebIds());
        lqw.eq(StringUtils.isNotBlank(bo.getQuydd()), ReportRhlqYcjldnfxjlbBsInfo::getQuydd, bo.getQuydd());
        lqw.eq(StringUtils.isNotBlank(bo.getRuhlqpzjdh()), ReportRhlqYcjldnfxjlbBsInfo::getRuhlqpzjdh, bo.getRuhlqpzjdh());
        lqw.eq(StringUtils.isNotBlank(bo.getJizlqcd()), ReportRhlqYcjldnfxjlbBsInfo::getJizlqcd, bo.getJizlqcd());
        lqw.eq(StringUtils.isNotBlank(bo.getJizlqzljbh()), ReportRhlqYcjldnfxjlbBsInfo::getJizlqzljbh, bo.getJizlqzljbh());
        lqw.eq(StringUtils.isNotBlank(bo.getJilcdjzl()), ReportRhlqYcjldnfxjlbBsInfo::getJilcdjzl, bo.getJilcdjzl());
        lqw.eq(StringUtils.isNotBlank(bo.getShiyff()), ReportRhlqYcjldnfxjlbBsInfo::getShiyff, bo.getShiyff());
        return lqw;
    }

    /**
     * 新增【请填写功能名称】
     */
    @Override
    public Boolean insertByBo(ReportRhlqYcjldnfxjlbBsInfoBo bo) {
        ReportRhlqYcjldnfxjlbBsInfo add = BeanUtil.toBean(bo, ReportRhlqYcjldnfxjlbBsInfo.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setExcelId(add.getExcelId());
        }
        return flag;
    }

    /**
     * 修改【请填写功能名称】
     */
    @Override
    public Boolean updateByBo(ReportRhlqYcjldnfxjlbBsInfoBo bo) {
        boolean flag;
        ReportRhlqYcjldnfxjlbBsInfo update = BeanUtil.toBean(bo, ReportRhlqYcjldnfxjlbBsInfo.class);
        validEntityBeforeSave(update);
        baseMapper.updateById(update);
        // TODO 执行子表
        flag = detailService.insertOrUpdateByNfxBo(bo.getNfxList(), update.getExcelId());
        return flag;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(ReportRhlqYcjldnfxjlbBsInfo entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除【请填写功能名称】
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
